package com.bbs.elsearch.service.impl;

import com.bbs.common.resp.ResultMsg;
import com.bbs.elsearch.dao.ArticleRepository;
import com.bbs.elsearch.entity.Articles;
import com.bbs.elsearch.request.Search;
import com.bbs.elsearch.service.ArticlesService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ArticlesServiceImp implements ArticlesService {

    @Resource
    private ArticleRepository articleRepository;


    @Override
    public ResultMsg getByArticleId(Integer articleId) {
        Articles  byArticleId = articleRepository.findByArticleId(articleId);
        return new ResultMsg("查询成功！",200,byArticleId);
    }

    @Override
    public ResultMsg getByArticleLike(Search search) {
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.must(QueryBuilders.matchQuery("articleTitle", search.getQ()));
        builder.should(QueryBuilders.matchQuery("articleContent",search.getQ()));
        FieldSortBuilder sort = SortBuilders.fieldSort("articleVisitors").order(SortOrder.DESC);
        PageRequest page = PageRequest.of(search.getPage(), search.getSize());
        String preTag = "<font color='#dd4b39'>";
        String postTag = "</font>";
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder()
        .withQuery(builder)
                .withHighlightFields(
                        new HighlightBuilder.Field("articleTitle").preTags(preTag).postTags(postTag),
                        new HighlightBuilder.Field("articleContent").preTags(preTag).postTags(postTag)
                )
        .withPageable(page)
        .withSort(sort);
        NativeSearchQuery query = queryBuilder.build();

        Page<Articles> articles = articleRepository.search(query);
        return new ResultMsg("查询成功！",200,articles);
    }

    @Override
    public ResultMsg findByUserId(Search search) {
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.must(QueryBuilders.termQuery("userId",search.getUserId()));
        FieldSortBuilder sort = SortBuilders.fieldSort("articleVisitors").order(SortOrder.DESC);
        PageRequest page = PageRequest.of(search.getPage(), search.getSize());
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .withPageable(page)
                .withSort(sort);
        NativeSearchQuery query = queryBuilder.build();

        Page<Articles> articles = articleRepository.search(query);
        return new ResultMsg("查询成功！",200,articles);
    }

    @Override
    public ResultMsg findByTagId(Search search) {
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.must(QueryBuilders.termQuery("tagId",search.getType()));
        FieldSortBuilder sort = SortBuilders.fieldSort("articleVisitors").order(SortOrder.DESC);
        PageRequest page = PageRequest.of(search.getPage(), search.getSize());
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .withPageable(page)
                .withSort(sort);
        NativeSearchQuery query = queryBuilder.build();

        Page<Articles> articles = articleRepository.search(query);
        return new ResultMsg("查询成功！",200,articles);
    }

    @Override
    public ResultMsg deleteByArticleId(Integer articleId) {
        articleRepository.deleteByArticleId(articleId);
        Articles byArticleId = articleRepository.findByArticleId(articleId);
        if (byArticleId != null){
            return new ResultMsg("删除失败！",500,false);
        }
        return new ResultMsg("删除成功！",200,true);
    }
}
